package com.zshan.clinic.common.enums;


import com.zshan.clinic.common.util.string.StringUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 就诊时段
 * @author lichen
 */
public enum VisitPeriodEnum {

    MORNING("1", "上午", LocalTime.of(0, 0), LocalTime.of(12, 0)),
    AFTERNOON("2", "下午", LocalTime.of(12, 0), LocalTime.of(18, 0)),
    EVENING("3", "晚上", LocalTime.of(18, 0), LocalTime.MAX);

    private String value;
    private String text;
    private LocalTime startTime;
    private LocalTime endTime;

    VisitPeriodEnum(String value, String text, LocalTime startTime, LocalTime endTime) {
        this.value = value;
        this.text = text;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public String getValue() {
        return value;
    }

    public String getText() {
        return text;
    }

    public LocalTime getStartTime() {
        return startTime;
    }

    public LocalTime getEndTime() {
        return endTime;
    }

    public static List<String> getAvailablePeriods(LocalTime localTime){
        List<String> availablePeriodList = new ArrayList<>();
        String currentPeriod = getPeriod(localTime);
        if(MORNING.getValue().equals(currentPeriod)){
            availablePeriodList.add(MORNING.getValue());
            availablePeriodList.add(AFTERNOON.getValue());
            availablePeriodList.add(EVENING.getValue());
        }else if (AFTERNOON.getValue().equals(currentPeriod)){
            availablePeriodList.add(AFTERNOON.getValue());
            availablePeriodList.add(EVENING.getValue());
        }else if (EVENING.getValue().equals(currentPeriod)){
            availablePeriodList.add(EVENING.getValue());
        }
        return availablePeriodList;
    }

    public static String getPeriod(LocalTime localTime) {
        if (!localTime.isBefore(MORNING.getStartTime()) && localTime.isBefore(MORNING.getEndTime())) {
            return MORNING.getValue(); // 上午
        } else if (!localTime.isBefore(AFTERNOON.getStartTime()) && localTime.isBefore(AFTERNOON.getEndTime())) {
            return AFTERNOON.getValue(); // 下午
        } else if (!localTime.isBefore(EVENING.getStartTime()) && localTime.isBefore(EVENING.getEndTime())) {
            return EVENING.getValue(); // 晚上
        }
        // 理论上不会走到这，但可加兜底
        return null;
    }

    public static VisitPeriodEnum getValue(String value) {
        if(StringUtil.isBlank(value)){
            return null;
        }
        VisitPeriodEnum[] arr$ = values();
        int len$ = arr$.length;
        for(int i$ = 0; i$ < len$; ++i$) {
            VisitPeriodEnum dot = arr$[i$];
            if (value.equals(dot.getValue())) {
                return dot;
            }
        }
        return null;
    }

    public static String getText(String value) {
        if(StringUtil.isBlank(value)){
            return null;
        }
        VisitPeriodEnum[] arr$ = values();
        int len$ = arr$.length;
        for(int i$ = 0; i$ < len$; ++i$) {
            VisitPeriodEnum dot = arr$[i$];
            if (value.equals(dot.getValue())) {
                return dot.getText();
            }
        }
        return null;
    }

    public static boolean isValue(String value) {
        if(StringUtil.isBlank(value)){
            return false;
        }
        VisitPeriodEnum [] arr$ = values();
        int len$ = arr$.length;
        for(int i$ = 0; i$ < len$; ++i$) {
            VisitPeriodEnum dot = arr$[i$];
            if (value.equals(dot.getValue())) {
                return true;
            }
        }
        return false;
    }

    public Map toMap() {
        Map map = new HashMap();
        map.put("value", this.value);
        map.put("text", this.text);
        map.put("startTime", this.startTime);
        map.put("endTime", this.endTime);
        return map;
    }

    public static List<Map> allToMap() {
        List<Map> list = new ArrayList();
        VisitPeriodEnum [] arr$ = values();
        int len$ = arr$.length;
        for(int i$ = 0; i$ < len$; ++i$) {
            VisitPeriodEnum dot = arr$[i$];
            list.add(dot.toMap());
        }
        return list;
    }

    /**
     * 将所有枚举项转为实体对象列表
     */
    public static List<VisitPeriod> allToList() {
        List<VisitPeriod> list = new ArrayList<>();
        for (VisitPeriodEnum visitPeriodEnum : values()) {
            list.add(
                    new VisitPeriod(
                            visitPeriodEnum.value,
                            visitPeriodEnum.text,
                            visitPeriodEnum.startTime,
                            visitPeriodEnum.endTime
                    )
            );
        }
        return list;
    }

    /**
     * 内部静态实体类：封装就诊时段信息
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class VisitPeriod {
        private String value;
        private String text;
        private LocalTime startTime;
        private LocalTime endTime;
    }


}
